/*
 * @lc app=leetcode.cn id=142 lang=javascript
 *
 * [142] 环形链表 II
 */

// @lc code=start
/**
 * Definition for singly-linked list.
 * function ListNode(val) {
 *     this.val = val;
 *     this.next = null;
 * }
 */

/**
 * @param {ListNode} head
 * @return {ListNode}
 */
var detectCycle = function(head) {
   let p1=head
   let p2=head
   let p3=head
   while(p2!==null){
       p1=p1.next
       p2=p2?.next?.next
       if(p1==p2)break
   }
   if(p2==null)return null
   if(p1==head)return head
   while(p3!==p1){
       p1=p1.next
       p3=p3.next
   }
    return p3   
};
// @lc code=end

